package gui;

import java.awt.*;
import java.awt.image.BufferedImage;

import javax.swing.*;

public class ControlPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	BufferedImage image;

	PowerButton powerButton = new PowerButton("poweroff.png", "poweron.png");
	HaltButton haltButton = new HaltButton("switch_off.png", "switch_on.png");
	SingleStepButton singleStepButton = new SingleStepButton("switch_off.png",
			"switch_on.png");
	RunButton runButton = new RunButton("run.png", "runclick.png");
	NextButton nextButton = new NextButton("run.png", "runclick.png");

	SelectButton[] selectButton = new SelectButton[14];
	MyInput input = new MyInput();
	MyOutput output = new MyOutput();

	// Register Lights
	public static RegisterLightGroup PC_Light = new RegisterLightGroup("PC");
	public static RegisterLightGroup MAR_Light = new RegisterLightGroup("MAR");
	public static RegisterLightGroup MBR_Light = new RegisterLightGroup("MBR");
	public static RegisterLightGroup IR_Light = new RegisterLightGroup("IR");
	public static RegisterLightGroup CC_Light = new RegisterLightGroup("CC");
	public static RegisterLightGroup OR_Light = new RegisterLightGroup("OR");
	public static RegisterLightGroup ADDR_Light = new RegisterLightGroup("ADDR");
	public static RegisterLightGroup MFR_Light = new RegisterLightGroup("MFR");
	public static RegisterLightGroup MSR_Light = new RegisterLightGroup("MSR");

	public static RegisterLightGroup R1_Light = new RegisterLightGroup("R1");
	public static RegisterLightGroup R2_Light = new RegisterLightGroup("R2");
	public static RegisterLightGroup R3_Light = new RegisterLightGroup("R3");
	public static RegisterLightGroup R4_Light = new RegisterLightGroup("R4");
	public static RegisterLightGroup RAR1_Light = new RegisterLightGroup("RAR1");
	public static RegisterLightGroup RAR2_Light = new RegisterLightGroup("RAR2");
	public static RegisterLightGroup DEVR_Light = new RegisterLightGroup("DEVR");

	public ControlPanel(BufferedImage image) {

		this.image = image;

		this.setLayout(null);
		// poweButton
		powerButton.setBounds(10, 10, 25, 25);

		// haltButton
		haltButton.setBounds(180, 66, 28, 18);

		// singleStepButton
		singleStepButton.setBounds(130, 102, 28, 18);

		// runButton
		runButton.setBounds(50, 72, 11, 11);

		// runButton
		nextButton.setBounds(250, 104, 11, 11);

		// selectButton
		String registerName[] = { "PC", "MAR", "MBR", "IR", "ADDR", "CC", "OR",
				"DEVR", "RAR1", "RAR2", "R1", "R2", "R3", "R4" };
		int i;
		for (i = 0; i < 14; i++) {
			selectButton[i] = new SelectButton();
			selectButton[i].num = i;
			selectButton[i].setBounds(386 + i / 7 * 71, 345 + i % 7 * 16, 71,
					16);
		}

		// Register Lights
		PC_Light.setPosition(52, 203);
		MAR_Light.setPosition(52, 220);
		MBR_Light.setPosition(52, 238);
		IR_Light.setPosition(52, 256);
		ADDR_Light.setPosition(52, 274);
		CC_Light.setPosition(52, 290);
		OR_Light.setPosition(52, 306);
		DEVR_Light.setPosition(52, 322);
		RAR1_Light.setPosition(52, 339);
		RAR2_Light.setPosition(214, 339);
		MFR_Light.setPosition(52, 358);
		MSR_Light.setPosition(52, 378);
		R1_Light.setPosition(52, 398);
		R2_Light.setPosition(52, 416);
		R3_Light.setPosition(52, 435);
		R4_Light.setPosition(52, 452);

		// Input textfield
		input.setBounds(334, 22, 372, 69);
		output.setBounds(421, 125, 280, 200);

		this.add(powerButton);
		this.add(haltButton);
		this.add(singleStepButton);
		this.add(runButton);
		this.add(nextButton);
		for (i = 0; i < 14; i++) {
			this.add(selectButton[i]);
		}
		this.add(PC_Light);
		this.add(MAR_Light);
		this.add(MBR_Light);
		this.add(IR_Light);
		this.add(ADDR_Light);
		this.add(CC_Light);
		this.add(OR_Light);
		this.add(DEVR_Light);
		this.add(RAR1_Light);
		this.add(RAR2_Light);
		this.add(MFR_Light);
		this.add(MSR_Light);
		this.add(R1_Light);
		this.add(R2_Light);
		this.add(R3_Light);
		this.add(R4_Light);

		this.add(input);
		this.add(output);

	}

	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;

		g2.drawImage(image, 0, 0, null);
	}
}